package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zero
 * @date 2020/7/6
 */

@Service
public class PageServiceImpl implements PageService {
    @Value("${pagepath}")
    private String pagepath;
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generateItemPage(String spuId) {
        //获取thymeleaf上下文对象
        Context context = new Context();
        //获取需要填充的数据
        Map<String, Object> itemData = this.getItemData(spuId);
        //填充数据
        context.setVariables(itemData);
        //指定文件输出的位置
        File dir = new File(pagepath);
        if (!dir.exists()){
            //如果文件夹不存在,则创建
            dir.mkdirs();
        }
        //定义输出流,进行文件输出
        File file = new File(dir+"/"+spuId+".html");
        Writer out = null;
        try {
            out = new PrintWriter(file);

            /**
             * 1.模板对象
             * 2.thymeleaf上下文对象
             * 3.流对象
             */
            templateEngine.process("item",context,out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private SkuFeign skuFeign;

    public Map<String,Object> getItemData(String spuId){
        //用于封装数据
        Map<String,Object> resultMap = new HashMap<>();
        //1.获取spu信息
        Spu spu = spuFeign.findSpuById(spuId).getData();
        //封装数据
        resultMap.put("spu",spu);
        //2.获取图片信息
        if (spu != null){
            if (StringUtils.isNotEmpty(spu.getImages())){
                //封装数据
                resultMap.put("imageList",spu.getImages().split(","));
            }
        }
        //3.获取分类信息
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        //封装数据
        resultMap.put("category1",category1);
        resultMap.put("category2",category2);
        resultMap.put("category3",category3);
        //4.获取sku商品信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        //封装数据
        resultMap.put("skuList",skuList);
        //5.获取规格信息
        Map specificationList = JSON.parseObject(spu.getSpecItems(), Map.class);
        resultMap.put("specificationList",specificationList);

        return resultMap;
    }
}
